0%

Practice on Linux 100

第一章 基本篇(7讲)

07 终端的使用

  • 图形终端
  • 命令行
  • 远程终端(SSH、VNC)

第二章 系统操作篇(18讲)

08 万能的帮助命令:man、help、info

  • 目录介绍

    • /root 用户的家目录
    • /home/username 普通用户的家目录
    • /etc 配置目录
    • /bin 命令目录
    • /sbin 管理命令目录
    • /usr/bin /usr/sbin 系统预装的其他命令
  • 帮助命令

    • man
      • man 7 man 打开man命令介绍的第7章
    • help (shell自带)
    • info
    • 使用网络资源

25 权限管理以及文件的特殊权限

chmod/chown

有一个文件夹或文件,属于ossgroup组不属于A用户,同时A属于ossgroup这个组。此时,属主与属组冲突,此时文件或文件夹的属主的权限算数,即A用户不能访问该文件或者文件夹。

特殊权限

SUID:用于二进制执行文件、执行命令时取得文件属主

SGID:用于目录,该目录下新建的文件和目录,仅root和自己可以删除

SBIT:用于目录,该目录下新建的文件和目录,仅root和自己可以删除

第三章 系统管理篇(29讲)

26 网络管理

  • 网络状态

  • 网络配置

  • 路由命令

  • 网络故障排除

  • 网络服务管理

  • 常用网络配置文件

  • net-tools VS iproute

    1. net-tools

      • ifconfig
      • route
      • netstat
    2. iproute2 (centos7 强推)

      • ip
      • ss
  • ifconfig

    • eth0 第一块网卡(网络接口)
    • 你的第一个网络接口可能叫做下面的名字

      • eno1 板载网卡
      • ens33 PCI-E网卡
      • enp0s3 无法获取物理信息的PCI-E网卡
      • CentOS 7 使用了一致性网络设备命名,以上都不匹配则使用eth0
        尽量时网卡一致,批量操作
    • 网卡命名规则受biosdevname和net.ifnames两个参数影响

    • 编辑/etc/default/grub文件,增加biosdevname=0 net.ifnames=0,grub是启动内核的工具,类似菜单
    • 更新grub
      • grub2-mkconfig -o /boot/grub2/grub.cfg,grub.cfg是启动真正被启动的
    • 重启
      • reboot
biosdevname net.ifnames 网卡名
默认 0 1 ens33
组合1 1 0 em1
组合2 0 0 eth0

27 查看网络配置

  • /sbin/ifconfig 普通用户用户执行命令

  • 查看网卡物理连接情况, 网卡的网线
    mii-tool eth0

  • 查看网关/路由,即网络地址需要连接其他区域
    route -n -n 参数不解析主机名
    ** 添加网关 route add -net 192.168.0.0 netmask 255.255.255.0 gw

28 修改网络配置

  • ifconfig <接口> <IP地址>[netmask 子网掩码]
  • ifup <接口>
  • ifdown <接口>
  • 网关设置 route
    route add default gw <网关ip> route add -host <指定ip> gw <网关ip>
    ** route add -net <指定网段> netmask <子网掩码> gw <网关ip>

29 网络故障排除命令

  • 主机连接

    ping:ping不通,网络中断,有防火墙
    traceroute:主机到目的主机的网络状况,如果还是有问题,证明网络质量不行

    1
    $ traceroute -w 1 www.baidu.com

    mtr:数据包是否有丢失

    1
    $ mtr www.baidu.com

    nslookup:域名访问

    1
    2
    $ yum install bind-utils
    $ nslookup www.baidu.com
  • 端口

    telnet:端口?

    1
    2
    3
    4
    5
    6
    7
    [root@localhost ~]# telnet www.baidu.com 80
    Trying 14.215.177.39...
    Connected to www.baidu.com.
    Escape character is '^]'.
    ^]
    telnet> quit
    Connection closed.

    tcpdump: 抓包

    1
    2
    3
    4
    tcpdump -i any -n port 80
    tcpdump -i any -n host www.baidu.com
    tcpdump -i any -n host www.baidu.com and port 80
    tcpdump -i any -n host www.baidu.com and port 80 w /tmp/filename.txt
  • 更加详细的跟踪
    netstat: 查看服务监听地址

    1
    2
    3
    4
    5
    6
    7
    $ netstat -ntpl
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7075/sshd
    tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7238/master
    tcp6 0 0 :::22 :::* LISTEN 7075/sshd
    tcp6 0 0 ::1:25 :::* LISTEN 7238/master

    ss: 查看服务监听地址

    1
    [root@localhost ~]# ss -ntpl

使用以上工具定位网络中的服务器是否正常

30 网络配置文件

  • ifcfg-eth0
  • /etc/hosts 控制网络传入参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    [root@localhost ~]# service network status
    Configured devices:
    lo eth0
    Currently active devices:
    lo eth0
    [root@localhost ~]# chkconfig --list network
    Note: This output shows SysV services only and does not include native
    systemd services. SysV configuration data might be overridden by native
    systemd configuration.

    If you want to list systemd services use 'systemctl list-unit-files'.
    To see services enabled on particular target use
    'systemctl list-dependencies [target]'.

    network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
    [root@localhost ~]# service network restart
    Restarting network (via systemctl): [ OK ]
    [root@localhost ~]# systemctl list-unit-files NetworkManager.service
    UNIT FILE STATE
    NetworkManager.service enabled

    1 unit files listed.
    [root@localhost ~]# chkconfig --level 2345 network off
    [root@localhost ~]# chkconfig --list network

    Note: This output shows SysV services only and does not include native
    systemd services. SysV configuration data might be overridden by native
    systemd configuration.

    If you want to list systemd services use 'systemctl list-unit-files'.
    To see services enabled on particular target use
    'systemctl list-dependencies [target]'.

    network 0:off 1:off 2:off 3:off 4:off 5:off 6:off
    [root@localhost ~]# chkconfig --level 2345 network on
    [root@localhost ~]# chkconfig --list network

    Note: This output shows SysV services only and does not include native
    systemd services. SysV configuration data might be overridden by native
    systemd configuration.

    If you want to list systemd services use 'systemctl list-unit-files'.
    To see services enabled on particular target use
    'systemctl list-dependencies [target]'.

    network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@localhost ~]# systemctl disable NetworkManager
    Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
    Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
    Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
    Removed symlink /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service.
    [root@localhost ~]# systemctl enable NetworkManager
    Created symlink from /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service to /usr/lib/systemd/system/NetworkManager.service.
    Created symlink from /etc/systemd/system/multi-user.target.wants/NetworkManager.service to /usr/lib/systemd/system/NetworkManager.service.
    Created symlink from /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service to /usr/lib/systemd/system/NetworkManager-dispatcher.service.
    Created symlink from /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service to /usr/lib/systemd/system/NetworkManager-wait-online.service.
    1
    2
    3
    4
    5
    [root@localhost network-scripts]# ls if*
    ifcfg-eth0 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team
    ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort
    ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel
    ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless
    1
    2
    3
    4
    5
    [root@localhost network-scripts]# nslookup
    > server
    Default server: 192.168.0.1
    Address: 192.168.0.1#53
    > exit
  • 主机名

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@localhost network-scripts]# hostname
    localhost.localdomain
    [root@localhost network-scripts]# hostnamectl set-hostname c7.test
    [root@localhost network-scripts]# hostname
    c7.test
    [root@localhost network-scripts]#
    [root@localhost network-scripts]# vi /etc/hosts
    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

    127.0.0.1 c7.test
    ~
    "/etc/hosts" 4L, 179C

31 软件包管理器的使用

  • 软件包管理器

  • rpm包和rpm命令

  • yum仓库

  • 源代码编译安装

  • 内核升级

  • grub配置文件

  • 包管理器是方便软件安装、卸载、解决软件依赖关系的重要工具

  • Centos、RedHat使用yum包管理器,软件安装格式为rpm

  • Debian、Ubuntu使用apt包管理器,软件安装包格式为deb

32 使用rpm命令安装软件包

  • rpm 包格式

    • vim-common-7.4.10-5.el7.x86_64.rpm
      软件名称、版本号、系统版本、平台
  • rpm命令常用常数

    • -q 查询软件包
    • -i 安装软件包
    • -e 卸载软件包
1
2
3
4
[root@c7 ~]# ls /dev/sr0
/dev/sr0
[root@c7 ~]# ls /dev/sr0 -l
brw-rw----. 1 root cdrom 11, 0 Oct 1 04:29 /dev/sr0

通过虚拟机挂在光盘

1
2
[root@c7 ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 is write-protected, mounting read-only

怎么解决缺少依赖包? 这就是使用rpm包的问题, 如果原来太多,就得先解决一个个依赖。

1
2
3
4
5
6
[root@c7 Packages]# rpm -i vim-enhanced-7.4.160-5.el7.x86_64.rpm
error: Failed dependencies:
libgpm.so.2()(64bit) is needed by vim-enhanced-2:7.4.160-5.el7.x86_64
libperl.so()(64bit) is needed by vim-enhanced-2:7.4.160-5.el7.x86_64
perl(:MODULE_COMPAT_5.16.3) is needed by vim-enhanced-2:7.4.160-5.el7.x86_64
vim-common = 2:7.4.160-5.el7 is needed by vim-enhanced-2:7.4.160-5.el7.x86_64

所以,我们会使用yum仓库

33 使用yum包管理器安装软件

阿里巴巴的镜像站
https://opsx.alibaba.com/

一般先备份/etc/yum.repos.d/CentOS-Base.repo,再wget

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@c7 Packages]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
[root@c7 Packages]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2019-10-01 04:55:06-- http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 113.96.109.96, 113.96.109.97, 119.147.111.226, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|113.96.109.96|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’

100%[=================================================================================================================>] 2,523 --.-K/s in 0s

2019-10-01 04:55:06 (70.7 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]
[root@c7 Packages]# yum makecache
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
1
yum update

34 通过源代码方式安装

  • 二进制安装

  • 源代码编译安装

    • wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
    • tar -zxf openresty-VERSION.tar.gz
    • cd openresty-VERSION/
    • ./configure –prefix=/usr/local/openresty //指定路径,否则会散布到很多其他文件夹中
    • gmake -j2 // 充分使用两个CPU核
    • gmake install

      以上第4步需要将所有依赖的包一个个yum install,所以挺麻烦的。

35 如何升级内核

46 磁盘分区和文件大小查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@c7 linux-5.3.2]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000e954b

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM

Disk /dev/mapper/centos-root: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-opt: 14.0 GB, 13950255104 bytes, 27246592 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

第四章 Shell篇(31讲)

第五章 文本操作篇(15讲)

86 元字符介绍

  • 正则表达式与文本搜索

    • 元字符
    • 扩展元字符
    • 文件的查找命令find
    • 文本内容的过滤查找grep

      1
      2
      3
      4
      5
      6
      7
      8
      9
      # 字符串:Do one thing at a time, and do well.
      # 匹配字符 an

      # . 匹配换行符外的任意单个字符
      # * 匹配任意一个跟在它前面的字符
      # [] 匹配方括号中的字符类中的任意一个
      # ^ 匹配开头
      # $ 匹配结尾
      # \ 转义后面的特殊字符

86 find/cut/sort/uniq / 扩展元字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# + 至少出现一次
# ? 匹配前面的正则表达式出现零次或者一次
# | 匹配|前面或者后面的字符串

[root@localhost ~] find /etc -regex .*wd$
/etc/security/opasswd
/etc/passwd
/etc/pam.d/passwd

# find: -type, -atime, -ctime, -mtime, -exec

[root@localhost test] touch filea
[root@localhost test] stat filea
File: ‘filea’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 34217662 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2019-09-12 23:31:16.494364300 -0400
Modify: 2019-09-12 23:31:16.494364300 -0400
Change: 2019-09-12 23:31:16.494364300 -0400
Birth: -

[root@localhost test] touch {1,2,3}.txt
[root@localhost test] ll
total 0
-rw-r--r--. 1 root root 0 Sep 12 23:55 1.txt
-rw-r--r--. 1 root root 0 Sep 12 23:55 2.txt
-rw-r--r--. 1 root root 0 Sep 12 23:55 3.txt
-rw-r--r--. 1 root root 0 Sep 12 23:31 filea
[root@localhost test] find ./ -name '*.txt' -exec rm -v {} \;
removed ‘./1.txt’
removed ‘./2.txt’
removed ‘./3.txt’

# cut
[root@localhost test]# cut -d ":" -f7 /etc/passwd | uniq -c
1 /bin/bash
4 /sbin/nologin
1 /bin/sync
1 /sbin/shutdown
1 /sbin/halt
14 /sbin/nologin
[root@localhost test]# cut -d ":" -f7 /etc/passwd | sort | uniq -c
1 /bin/bash
1 /bin/sync
1 /sbin/halt
18 /sbin/nologin
1 /sbin/shutdown
[root@localhost test]# cut -d ":" -f7 /etc/passwd | sort | uniq -c | sort -r
1 /sbin/shutdown
1 /sbin/halt
1 /bin/sync
1 /bin/bash
18 /sbin/nologin

解决find: paths must precede expression

sed/awk - 行编辑器介绍

vim 交互式、文件操作模式
sed\awk 非交互式、行操作模式

sed 一般用户对文本内容的替换

1
sed '/user1/s/user1/u1/' /etc/passwd

AWK 一般用于对文本内容进行统计,安祖要

1
2
cut -d ":" -f7 /etc/passwd | sort | uniq -c | sort -r
awk -F: '/wd$/{print $1}' /etc/passwd

第六章 服务管理篇(18讲)